<template>
  <el-main>
    <el-form :model="parms" ref="deptForm" :inline="true" size="small">
      <el-form-item label="">
        <el-input
          v-model="parms.searchName"
          placeholder="请输入部门名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="doSearch">查询</el-button>
        <el-button icon="el-icon-close" @click="doReset">重置</el-button>
        <el-button
          v-if="hasPerm('sys:addDepartment')"
          @click="addDept"
          type="primary"
          icon="el-icon-plus">
          新增
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableList"
      border
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      :default-expand-all="true"
      :tree-props="{ children: 'children' }"
      stripe>
      <el-table-column prop="name" label="部门名称"></el-table-column>
      <el-table-column prop="deptCode" label="部门编码"></el-table-column>
      <el-table-column prop="deptPhone" label="部门电话"></el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <!--slot-scope="scope"这个是自定义传参,这个scope可以随便起名字-->
        <template slot-scope="scope">
          <el-button
            v-if="hasPerm('sys:editDept')"
            icon="el-icon-edit-outline"
            type="primary"
            size="small"
            @click="editHandler(scope.row)">
            编辑
          </el-button>
          <el-button
            v-if="hasPerm('sys:deleteDept')"
            icon="el-icon-close"
            type="danger"
            size="small"
            @click="deleteHandler(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <useDialog ref="useDialog" @onConfirm="onConfirm"></useDialog>
  </el-main>
</template>

<script>
import {
  getDeptListApi,
  addDeptSaveApi,
  deleteDeptApi,
  editDeptSaveApi
} from '@/api/department'
import useDialog from './useDialog.vue'
export default {
  components: {
    // SysDialog
    useDialog
  },
  data() {
    return {
      // 部门列表数据
      tableList: [],
      // 搜索关键字数据
      parms: {
        searchName: ''
      }
    }
  },
  created() {
    this.getDeptList()
  },
  methods: {
    // 新增部门对话框的事件
    // 查询部门
    doSearch() {
      console.log('查询部门')
      this.getDeptList()
    },
    doReset() {
      console.log('重置')
      this.$resetForm('deptForm', this.parms)
      this.getDeptList()
    },
    // 新增部门
    addDept() {
      this.$refs.useDialog.show('0')
    },
    // 取消
    // onClose () {
    //   this.addDialog.visible = false
    // },
    // 确认
    async onConfirm(val) {
      console.log(val)
      let res = null
      if (val.editType === '0') {
        res = await addDeptSaveApi(val)
        // 新增
      } else {
        // 编辑
        res = await editDeptSaveApi(val)
      }
      console.log(res)
      if (res && res.code === 200) {
        this.$message.success(res.msg)
        this.getDeptList()
        // this.addDialog.visible = false
      }
    },
    // 获取部门列表
    async getDeptList() {
      let res = await getDeptListApi(this.parms)
      console.log(res)
      this.tableList = res.data
    },
    editHandler(row) {
      this.$refs.useDialog.show('1', row)
    },
    async deleteHandler(row) {
      // console.log(row)
      // confirm 的值为true(确定)或者undefined(取消)
      let confirm = await this.$myconfirm('确认删除么?')
      console.log(confirm)
      if (confirm) {
        let parm = {
          id: row.id
        }
        let res = await deleteDeptApi(parm)
        if (res && res.code === 200) {
          this.$message.success(res.msg)
          this.getDeptList()
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
